package cn.tedu.truetaste.service.impl;

import cn.tedu.truetaste.ex.ServiceException;
import cn.tedu.truetaste.mapper.CategoryMapper;
import cn.tedu.truetaste.mapper.DishesMapper;
import cn.tedu.truetaste.mapper.SpuDetailMapper;
import cn.tedu.truetaste.mapper.SpuMapper;
import cn.tedu.truetaste.pojo.dto.SpuAddNewDTO;
import cn.tedu.truetaste.pojo.entity.Spu;
import cn.tedu.truetaste.pojo.entity.SpuDetail;
import cn.tedu.truetaste.pojo.query.SpuQuery;
import cn.tedu.truetaste.pojo.vo.*;
import cn.tedu.truetaste.restful.JsonPage;
import cn.tedu.truetaste.service.ISpuService;
import cn.tedu.truetaste.util.IdUtils;
import cn.tedu.truetaste.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SpuServiceImpl implements ISpuService {

    @Autowired
    private DishesMapper dishesMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Override
    public void addNew(SpuAddNewDTO spuAddNewDTO) {
        log.debug("开始处理【新增SPU】的业务，参数：{}", spuAddNewDTO);

        // 检查菜品是否存在
        Long dishesId = spuAddNewDTO.getDishesId();
        DishesStandardVO dishes = dishesMapper.getStandardById(dishesId);
        if (dishes == null) {
            String message = "新增SPU失败，选择的菜品不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查类别是否存在，是否被禁用，是否为父级类别
        Long categoryId = spuAddNewDTO.getCategoryId();
        CategoryStandardVO category = categoryMapper.getStandardById(categoryId);
        if (category == null) {
            String message = "新增SPU失败，选择的类别不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        /*if (category.getEnable() == 0) {
            String message = "新增SPU失败，选择的类别已经被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        if (category.getIsParent() == 1) {
            String message = "新增SPU失败，选择的类别包含子级类别，不允许用于新增SPU！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }*/

        // 创建Spu对象
        Spu spu = new Spu();
        // 调用BeanUtils.copyProperties()复制属性
        BeanUtils.copyProperties(spuAddNewDTO, spu);
        // 补全Spu对象中属性的值：id >>> 暂时写死为某个值
        spu.setId(IdUtils.generateId());
        // 补全Spu对象中属性的值：dishesName
        spu.setDishesName(dishes.getName());
        // 补全Spu对象中属性的值：categoryName
        spu.setCategoryName(category.getName());
        // 补全Spu对象中属性的值：sales, commentCount, positiveCommentCount >>> 0
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全Spu对象中属性的值：isDelete, isPublished >>> 0
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        // 补全Spu对象中属性的值：isNewArrival >>> 1
        spu.setIsNewArrival(1);
        // 补全Spu对象中属性的值：isChecked >>> 0, checkUser >>> null, gmtCheck >>> null
        spu.setIsChecked(0);
        spu.setCheckUser(null);
        spu.setGmtCheck(null);
        // 调用SpuMapper对象的insert()执行插入
        int rows = spuMapper.insert(spu);
        if (rows != 1) {
            String message = "新增SPU失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        // 创建SpuDetail对象
        SpuDetail spuDetail = new SpuDetail();
        // 补全SpuDetail对象中属性的值：spuId >>> spu.getId()
        spuDetail.setSpuId(spu.getId());
        // 补全SpuDetail对象中属性的值：detail >>> 来自参数对象
        spuDetail.setDetail(spuAddNewDTO.getDetail());
        // 调用SpuDetailMapper对象的insert()执行插入
        rows = spuDetailMapper.insert(spuDetail);
        if (rows != 1) {
            String message = "新增SPU失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public JsonPage<SpuListItemVO> listSpuByCategoryId(Long categoryId, Integer page, Integer pageSize) {
        SpuQuery spuQuery=new SpuQuery();
        spuQuery.setCategoryId(categoryId);
        PageHelper.startPage(page,pageSize);
        List<SpuListItemVO> spuListItemVOs = spuMapper.listByCustomCondition(spuQuery);
        return JsonPage.restPage(new PageInfo<>(spuListItemVOs));
    }

    /**
     * 和已有方法重复
     * @param id
     * @return
     */
    @Override
    public SpuStandardVO getSpuById(Long id) {
        SpuStandardVO spuStandardVO = spuMapper.getById(id);
        if (spuStandardVO == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "查询SPU详情失败，尝试访问的SPU数据不存在！");
        }
        return spuStandardVO;
    }

    @Override
    public SpuDetailStandardVO getSpuDetailById(Long spuId) {
        SpuDetailStandardVO spuDetailStandardVO = spuDetailMapper.getBySpuId(spuId);
        return spuDetailStandardVO;
    }

    @Override
    public JsonPage<Spu> getSpuByPage(Integer pageNum, Integer pageSize) {
            PageHelper.startPage(pageNum,pageSize);
            List<Spu> list=spuMapper.findAllList();
            return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public List<SpuListItemVO> listBySpuDetailId(Long spuDetailId) {
        log.debug("开始处理【根据spu查询spu详情列表】的业务，参数：{}", spuDetailId);
        return spuMapper.listBySpuDetailId(spuDetailId);
    }

}
